@page "/counterdapr"
@inject DaprClient _daprClient

<h1>Counter Dapr</h1>

<p>Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
        
    private const string CounterStore = "counter-store";

    protected override async Task OnInitializedAsync()
    {
        currentCount = await _daprClient.GetStateAsync<int>(CounterStore, "count");

        await base.OnInitializedAsync();
    }

    private int currentCount;

    private async Task IncrementCount()
    {
        currentCount++;

        await _daprClient.SaveStateAsync(CounterStore, "count", currentCount);
        
        await _daprClient.PublishEventAsync("pubsub", "CounterIncremented", currentCount);
    }
}